const pearson = {
  /**
   * @description 获取平均值
   * @param {Object} datas
   */
  getMeans (datas) {
    let sum = 0.0
    datas.forEach(item => (sum += item))
    return sum / datas.length
  },

  /**
   * @description 生成分母
   * @param {Object} xData0
   * @param {Object} xMeans
   * @param {Object} yData
   * @param {Object} yMeans
   */
  generateDenomiator (xData, xMeans, yData, yMeans) {
    let xSum = 0.0
    xData.forEach(item => (xSum += (item - xMeans) * (item - xMeans)))
    let ySum = 0.0
    yData.forEach(item => (ySum += (item - yMeans) * (item - yMeans)))
    return Math.sqrt(xSum) * Math.sqrt(ySum)
  },

  /**
   * @description 生成分子
   * @param {Object} xData
   * @param {Object} xMeans
   * @param {Object} yData
   * @param {Object} yMeans
   */
  generateNumerator (xData, xMeans, yData, yMeans) {
    let numerator = 0.0
    for (var i in xData) {
      numerator += (xData[i] - xMeans) * (yData[i] - yMeans)
    }
    return numerator
  },

  getPearsonBias (xData, yData) {
    let xMeans = 0.0
    let yMeans = 0.0
    let numerator = 0.0
    let denominator = 0.0

    // x,y平均值
    xMeans = this.getMeans(xData)
    yMeans = this.getMeans(yData)
    // 计算皮尔逊系数分子
    numerator = this.generateNumerator(xData, xMeans, yData, yMeans)
    // 计算皮尔逊系数分母
    denominator = this.generateDenomiator(xData, xMeans, yData, yMeans)
    // 计算皮尔逊系数
    return numerator / denominator
  }
}

export default pearson
